package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode53 {
    public int maxSubArray(int[] nums) {
        int[] dp=new int[nums.length];
        dp[0]=nums[0];
        int res=nums[0];
        for (int i = 1; i < nums.length ; i++) {
            dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
            res=Math.max(dp[i],res);
        }
        return res;
    }
    public int maxSubArrayPlus(int[] nums) {
            int res=nums[0];
            int tmp=nums[0];
            int len= nums.length;
            for (int i = 1; i < len; i++) {
                tmp=Math.max(nums[i],tmp+nums[i]);
                res=Math.max(res,tmp);
            }
            return res;
    }

    public static void main(String[] args) {
        int[] nums=new int[]{-2,1,-3,4,-1,2,1,-5,4};
        leetCode53 leetCode53=new leetCode53();
        System.out.println(leetCode53.maxSubArrayPlus(nums));
    }
}
